﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_TypeManage : BaseAdmin
{
    ZSql zsql = new ZSql();
    Function f = new Function();

    bool IsGridEdit = false;
    bool IsGridDeleted = false;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblwz.Text = f.getModuleidAdd(hfxs.Value);
            this.SetRule();
            gdata();
            initData();


        }
    }
    protected void lnkbtnFlag_Click(object sender, EventArgs e)
    {
        string BrandId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("AddBrand.aspx?BrandId=" + BrandId + "&moduid=" + hfxs.Value + "&action=flag");

    }

    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string BrandId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("AddBrand.aspx?BrandId=" + BrandId + "&moduid=" + hfxs.Value + "&action=update");

    }
    protected void btndel_Click(object sender, EventArgs e)
    {
        ZSql del = new ZSql();
        int n = 0;
        if (this.GvDataBand.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataBand.Rows.Count; i++)
            {
                CheckBox ck = GvDataBand.Rows[i].Cells[9].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {

                    del.Open("delete Brand  where BrandId=" + this.GvDataBand.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);

                }
                else
                {
                    n++;
                }

                if (n == this.GvDataBand.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }

            }
        }
    }




    protected void btnSelec_Click(object sender, EventArgs e)
    {
        this.SetRule();
        string sql = "select b.BrandId,b.AuditState,a.ComoditiesType,b.Brand,b.BrandOrder,b.usTime,b.UserName,b.Brandbh,(SELECT COUNT(*) FROM CS_Stock  WHERE BrandId = b.BrandId ) as Stock from ComoditiesType as a ,Brand as b where a.TypeId=b.TypeId";
        if (Common.FormatParameter(txtselectype.Text) != "")
        {

            sql += " and a.ComoditiesType like'%" + Common.FormatParameter(txtselectype.Text) + "%'";



        }
        if (Common.FormatParameter(txtselecName.Text) != "")
        {
            sql += " and b.Brandbh like'%" + Common.FormatParameter(txtselecName.Text) + "%'";
        }


        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddBrand.aspx?moduid=" + hfxs.Value + "&action=add");
    }
    private void gdata()
    {
        string sql = "select b.BrandId,b.AuditState,a.ComoditiesType,b.Brand,b.BrandOrder,b.usTime,b.UserName,b.Brandbh,(SELECT COUNT(*) FROM CS_Stock  WHERE BrandId = b.BrandId ) as Stock from ComoditiesType as a ,Brand as b where a.TypeId=b.TypeId order by a.TypeId desc";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;

    }
    private void initData()
    {




    }

    void SetRule()
    {

        if (!base.GetValidateLoginRule(Gift.Model.ENUM.ERule.StoreProductBrandAdd))
        {
            this.btnAdd.Visible = false;
        }
        if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.StoreProductBrandEdit))
        {
            IsGridEdit = true;
           
        }
        if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.StoreProductBrandDeleted))
        {
            IsGridDeleted = true;
        }
    }

    protected void btnallflag_Click(object sender, EventArgs e)
    {
        ZSql allflag = new ZSql();
        int n = 0;
        if (this.GvDataBand.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataBand.Rows.Count; i++)
            {
                CheckBox ck = GvDataBand.Rows[i].Cells[8].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {
                    int flag = 1;
                    allflag.Open("update Brand set AuditState=" + flag + " where BrandId=" + this.GvDataBand.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('审核成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvDataBand.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }
    protected void GvDataBand_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //f.checkLevels(Request.Cookies["user"].Values["roleid"], hfxs.Value, 8, GvDataBand, btndel, btnallflag, btnAdd);

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (!IsGridDeleted)
            {
                (e.Row.Cells[7].FindControl("lnkBtnDeleted") as LinkButton).Visible = false;
            }
            if (!IsGridEdit)
            {
                (e.Row.Cells[7].FindControl("lnkBtnEdit") as LinkButton).Visible = false;
            }
        }
    }
}
